package Service;

import java.util.ArrayList;
import java.util.Date;

import vo.Comment;
import vo.Status;
import Utils.DateUtil;
import Utils.MathUtil;
import dao.CommentDao;
import dao.StatusDao;

public class CommentService {

	/**
	 * @author 韩 闻文 total comments
	 * @return
	 */
	public static int getCount() {
		return CommentDao.getCountByCondition(null);
	}

	/**
	 * @author 韩 闻文 comments count of a user
	 * @param user_id
	 * @return
	 */
	public static int getCountByUser(String userId) {
		Comment comment = new Comment();
		comment.setUserId(userId);
		return CommentDao.getCountByCondition(comment);
	}

	/**
	 * @author 韩 闻文 total comments in a time period
	 * @return
	 */
	public static int getCountByTimeRange(String startTime, String endTime) {
		Status status = new Status();
		status.setStartTime(startTime);
		status.setEndTime(endTime);
		return StatusDao.getCountByCondition(status);
	}

	/**
	 * @author 韩 闻文 total comments in a period of time of a user
	 * @return
	 */
	public static int getCountByTimeRangeUserId(String startTime, String endTime, String userId) {
		Status status = new Status();
		status.setUserId(userId);
		status.setStartTime(startTime);
		status.setEndTime(endTime);
		return StatusDao.getCountByCondition(status);
	}

	/**
	 * @author 韩 闻文 get comment count by source type
	 * @return
	 */
	public static int getCountBySourceName(String sourceName) {
		Comment comment = new Comment();
		comment.setSourceName(sourceName);
		return CommentDao.getCountByCondition(comment);
	}

	/**
	 * @author 韩 闻文 get all comments  belonged to the
	 *         same user
	 * @param userId
	 * @return
	 */
	public static ArrayList<Comment> getByUserId(String userId) {
		Comment comment = new Comment();
		comment.setUserId(userId);
		return CommentDao.getByCondition(comment);
	}

	/**
	 * @author 韩 闻文 get count of all comments belonged
	 *         to the same user
	 * @param userId
	 * @return
	 */
	public static int getCountByUserId(String userId) {
		Comment comment = new Comment();
		comment.setUserId(userId);
		return CommentDao.getCountByCondition(comment);
	}
	/**
	 * @author 韩 闻文 created time of all comments of a certain user
	 * @param userId
	 * @return
	 */
	public static ArrayList<Date> getCommentTimesByUserId(String userId) {
		ArrayList<Date> result = new ArrayList<Date>();
		Comment comment=new Comment();
		comment.setUserId(userId);
		ArrayList<Comment> comments=CommentDao.getByCondition(comment);
		for(int i=0;i<comments.size();i++)
			result.add(comments.get(i).getCreatedAt());
		return result;
	}
	/**
	 * @author 韩 闻文 average comment interval of a certain user
	 * @param userId
	 * @return
	 */
	public static String getAVGCommentFrequencyByUserId(String userId) {
		ArrayList<Date> dateTimes = getCommentTimesByUserId(userId);
		ArrayList<Integer> intervals = DateUtil.getIntervals(dateTimes);
		double averageInterval = MathUtil.average(intervals);
		return String.valueOf(averageInterval);
	}


}
